AStarView = {}
local self = AStarView

self.view = UIBase()
self.view.name = "AStarView"
self.view:AddPath("ui/panels/astar.gdmec")
self.view.onInit = function()
    self:OnInit()
end

self.view.onUnit = function()
    self:OnUnit()
end

function self:Close()
    self.view:Close()
end

function self:Open()
    self.view:Open()
end

function self:OnInit()
    self.childs = {}
    self.var_from_x = AStarConstant.StartPos.X
    self.var_from_y = AStarConstant.StartPos.Y
    self.ui_btn_close = self.view.go.transform:Find("BtnClose"):GetComponent(typeof(UnityEngine.UI.Button))
    
    self.ui_btn_close.onClick:AddListener(
        function()
            self.ui_btn_close.gameObject.transform:DOShakeScale(0.1);
            self:Close()
        end
    )

    self.ui_btn_nav = self.view.go.transform:Find("BtnNav"):GetComponent(typeof(UnityEngine.UI.Button))
    self.ui_nav_tf = self.ui_btn_nav.transform:Find("BtnNavAct")
    self.ui_btn_nav.onClick:AddListener(
        function()
            AudioManager.PlayMusic("10001")
            self.ui_btn_nav.gameObject.transform:DOShakeScale(0.1);
            self:BtnMod(AStarConstant.AStarViewBtnType.Nav)
        end
    )

    self.ui_btn_wall = self.view.go.transform:Find("BtnWall"):GetComponent(typeof(UnityEngine.UI.Button))
    self.ui_wall_tf = self.ui_btn_wall.transform:Find("BtnWallAct")
    self.ui_btn_wall.onClick:AddListener(
        function()
            AudioManager.PlayMusic("10001")
            self.ui_btn_wall.gameObject.transform:DOShakeScale(0.1);
            self:BtnMod(AStarConstant.AStarViewBtnType.Wall)
        end
    )

    self.ui_btn_start = self.view.go.transform:Find("BtnStart"):GetComponent(typeof(UnityEngine.UI.Button))
    self.ui_start_tf = self.ui_btn_start.transform:Find("BtnStartAct")
    self.ui_btn_start.onClick:AddListener(
        function()
            AudioManager.PlayMusic("10001")
            self.ui_btn_start.gameObject.transform:DOShakeScale(0.1);
            self:BtnMod(AStarConstant.AStarViewBtnType.Start)
        end
    )

    self.ui_btn_reset = self.view.go.transform:Find("BtnReset"):GetComponent(typeof(UnityEngine.UI.Button))
    self.ui_reset_tf = self.ui_btn_reset.transform:Find("BtnResetAct")
    self.ui_btn_reset.onClick:AddListener(
        function()
            AudioManager.PlayMusic("10001")
            self.ui_btn_reset.gameObject.transform:DOShakeScale(0.1);
            self:BtnMod(AStarConstant.AStarViewBtnType.ReSet)
        end
    )

    self.ui_root = self.view.go.transform:Find("Root")
    self.ui_grid_prefab = self.view.go.transform:Find("Root/GirdPrefab")
    self.ui_grid_prefab.gameObject:SetActive(false)

    AStarControl:SetDiff()
    self:BtnMod(AStarConstant.AStarViewBtnType.Nav)
    self:UpView()
end

function self:BtnMod(type)
    self.mod_type = type
    self.ui_nav_tf.gameObject:SetActive(type == AStarConstant.AStarViewBtnType.Nav)
    self.ui_wall_tf.gameObject:SetActive(type == AStarConstant.AStarViewBtnType.Wall)
    self.ui_start_tf.gameObject:SetActive(type == AStarConstant.AStarViewBtnType.Start)
    self.ui_reset_tf.gameObject:SetActive(type == AStarConstant.AStarViewBtnType.ReSet)
    if type == AStarConstant.AStarViewBtnType.ReSet then 
        self.var_from_x = AStarConstant.StartPos.X
        self.var_from_y = AStarConstant.StartPos.Y
        AStarControl:ReSetGrid()
        self:UpView()
    end
end
function self:UpView()
    for k, v in pairs(self.childs) do
        v.go.gameObject:SetActive(false)
    end
    local diff_args = AStarConstant.DiffArgs[AStarControl.diff_lev]
    local idx = 0
    local child = nil
    local grid = nil
    for y = 1, diff_args.Y do
        for x = 1, diff_args.X do
            idx = x + (y - 1) * diff_args.X
            child = self:MakeChild(idx, x, y)
            grid = AStarControl:GetGrid(x, y)
            if grid then
                if grid.grid_type == AStarConstant.GripType.Start then
                    child.start.gameObject:SetActive(true)
                elseif grid.grid_type == AStarConstant.GripType.Wall then
                    child.wall.gameObject:SetActive(true)
                end
            end
        end
    end
end


function self:MakeChild(idx, x, y)
    local child = self.childs[idx]
    if child == nil then
        local go = UnityEngine.GameObject.Instantiate(self.ui_grid_prefab, self.ui_root)
        go.name = x .."," ..y
        child = {
            go = go,
            txt_idx = go.transform:Find("Idx"):GetComponent(typeof(UnityEngine.UI.Text)),
            act = go.transform:Find("Act"),
            start = go.transform:Find("Start"),
            endft = go.transform:Find("End"),
            wall = go.transform:Find("Wall"),
            btn = go:GetComponent(typeof(UnityEngine.UI.Button)),
            x = x,
            y = y
        }
        child.btn.onClick:AddListener(
            function()
                self:ClickGrid(x, y)
            end
        )
        self.childs[idx] = child
    end
    child.act.gameObject:SetActive(false)
    child.start.gameObject:SetActive(false)
    child.wall.gameObject:SetActive(false)
    child.endft.gameObject:SetActive(false)
    child.go.gameObject:SetActive(true)
    return child
end

function self:ClickGrid(x, y)
    if self.mod_type == AStarConstant.AStarViewBtnType.Nav then
        self:UpView()
        self:DoNav(x, y)
    elseif self.mod_type == AStarConstant.AStarViewBtnType.Wall then
        AStarControl:SetGridType(x, y, AStarConstant.GripType.Wall)
        self:UpView()
    elseif self.mod_type == AStarConstant.AStarViewBtnType.Start then
        self.var_from_x = x
        self.var_from_y = y
        AStarControl:SetGridType(x, y, AStarConstant.GripType.Start)
        self:UpView()
    end
end


function self:DoNav(x, y)
    local diff_args = AStarConstant.DiffArgs[AStarControl.diff_lev]
    local paths = AStarControl:Nav(self.var_from_x, self.var_from_y, x, y)
    local grid = nil
    local child = nil
    local idx = 0
    for i = #paths, 1, -1 do
        grid = paths[i]
        idx = grid.x + (grid.y - 1) * diff_args.X
        child = self:MakeChild(idx, grid.x, grid.y)
        if child then
            if grid.grid_type == AStarConstant.GripType.End then 
                child.endft.gameObject:SetActive(true)
            elseif grid.grid_type == AStarConstant.GripType.Node then 
                child.act.gameObject:SetActive(true)
            elseif grid.grid_type == AStarConstant.GripType.Start then 
                child.start.gameObject:SetActive(true)
            end    
        end
    end
end

function self:OnUnit()
end
